package com.jn.algorithm.day017.question50;

/**
 * @author 江南大学1033190417
 * @date 2022/5/8 17:12
 */
public class Solution {
    //栈空间不足
    public double myPow(double x, int n) {
        if (n == 0) {
            return 1;
        } else if (n > 0) {
            return pow(x, n);
        } else {
            return 1 / pow(x, -n);
        }

    }

    public double pow(double x, int n) {
        if (n == 1) {
            return x;
        }
        return x * myPow(x, n - 1);
    }

    //分治
    public double myPow2(double x, int n) {
        return (long) n >= 0 ? pow2(x, n) : 1 / pow2(x, -(long) n);
    }

    public double pow2(double x, long n) {
        if (n == 0) {
            return 1.0;
        }
        double y = pow2(x, n / 2);
        return n % 2 == 0 ? y * y : y * y * x;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        double x = 0.00001;
        int n = Integer.MAX_VALUE;
        System.out.println(solution.myPow2(x, n));
    }
}
